package com.example.supermarket.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.supermarket.entity.Member;
import com.example.supermarket.entity.Order;
import com.example.supermarket.entity.OrderAndGoods;
import com.example.supermarket.mapper.OrderMapper;
import com.example.supermarket.service.MemberOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>会员查看消费记录模块</p>
 * @author Bosen
 * @date 2021/10/17 0:22
 */
@RestController
public class MemberOrderController {

    @Autowired
    MemberOrderService service;

    @Autowired
    OrderMapper orderMapper;

    @RequestMapping("/member/order")
    public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {
        // 获取当前会员信息
        Member member = (Member) request.getSession().getAttribute("member");
        // 获取会员的消费记录
        List<OrderAndGoods> order = service.getOrderAndGoodsList(member.getId());

        String orderJson = JSONArray.toJSONString(order);
        modelAndView.addObject("orderJson",orderJson);
        modelAndView.setViewName("member/order");
        return modelAndView;
    }

    /**
     * <p>修改支付状态</P>
     */
    @RequestMapping("/member/update/orderpay")
    public int updateOrderPay(String orderId){
        return  orderMapper.updateOrder(orderId, 1, System.currentTimeMillis());
    }

    /**
     * <p>删除订单</P>
     */
    @RequestMapping("/member/del/order")
    public int delOrder(int id){
        return orderMapper.deleteById(id);
    }
}
